package com.wwwarehouse.taskcenter.fragment.job_point.step2.child;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.ItemInfoResponseBean;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;
import com.wwwarehouse.taskcenter.eventbus_event.JobPointSetnumberEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 201711/1
 */

public class ChooseAddWayChildItemChangeByAllSetNumPageFragment extends CommonBasePagerFragment {
    private ArrayList<ItemInfoResponseBean.ItemDetailsBean> mItemDetailsList;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    private void initView() {

    }

    private void initData() {

    }

    private void initEvent() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayChildItemChangeByAllSetNumPageFragment) {
            if (TaskCenterCommon.getInstance().isDetails()) {
                mActivity.setTitle(R.string.view_details);
            } else {
                if (TaskCenterCommon.getInstance().isStart()) {
                    mActivity.setTitle(R.string.has_choose_start_value);
                } else {
                    mActivity.setTitle(R.string.has_choose_end_value);
                }
            }
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        initPager();
    }

    @Override
    public void onDownPull() {
    }



    private void initPager() {
        mItemDetailsList = getArguments().getParcelableArrayList("selectList");

        Bundle bundle = new Bundle();
        try {
            setData(mItemDetailsList.size(), 10, new ChooseAddWayChildItemChangeByAllSetNumDetailsFragment().getClass().getName(), bundle, mItemDetailsList);

            if (mItemDetailsList.size() > 0) {
                initConfirmBtn();
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.showErrLog("error:"+e.getMessage());
        }
    }

    @Override
    public void onRefreshListener() {
    }

    @Override
    public void onLoadSuccess(String s, int i) {
    }

    @Override
    public void onLoadError(String s) {

    }

    public void onEventMainThread(JobPointSetnumberEvent event) {
        if (peekFragment() instanceof ChooseAddWayChildItemChangeByAllSetNumPageFragment) {
            for (ItemInfoResponseBean.ItemDetailsBean itemDetailsBean : mItemDetailsList) {
                if (itemDetailsBean.getCount() > 0) {
                    mConfirmBtn.setEnabled(true);

                    return;
                }
            }

            mConfirmBtn.setEnabled(false);
        }
    }

    private void initConfirmBtn() {
        showConfirmButton();
        setConfirmBtnText(mActivity.getString(R.string.confirm));

        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popFragment();
                popFragment();

                buildItemChangeMap(mItemDetailsList);

                // 反向传值
                Intent intent = new Intent("ChooseAddWayChildItemChangeByAll");
                mActivity.sendBroadcast(intent);
            }
        });
    }

    private void buildItemChangeMap(List<ItemInfoResponseBean.ItemDetailsBean> selectBean) {
        Map map = new HashMap();
        List<Map<String, String>> list = new ArrayList<>();

        for (ItemInfoResponseBean. ItemDetailsBean itemDetailsBean : selectBean) {
            if (itemDetailsBean.isSelect() && itemDetailsBean.getCount() > 0) {
                Map<String, String> listMap = new HashMap<>();

                listMap.put("key", itemDetailsBean.getAbstractObjectUkid() + "+" + itemDetailsBean.getCount() + "+" + itemDetailsBean.getUnitUkid());
                listMap.put("value", itemDetailsBean.getName() + "(" + itemDetailsBean.getCount() + itemDetailsBean.getUnitName() + ")");

                list.add(listMap);
            }
        }

        map.put("list", list);
        map.put("type", "ITEM");

        if (TaskCenterCommon.getInstance().isStart()) {
            TaskCenterCommon.getInstance().setStartMap(map);
        } else {
            TaskCenterCommon.getInstance().setEndMap(map);
        }
    }
}
